package ws.performancetest.tests.jdbcdriver;

import ws.performancetest.tests.jdbcdriver.runnable.ResultSetRunnable;
import ws.jdbc.concurrent.driver.sqlserver.CDriver;
import java.sql.Connection;
import java.sql.SQLException;
import ws.performancetest.api.annotation.PerformanceTest;
import ws.performancetest.api.exception.TestExecutionException;
import ws.performancetest.api.exception.TestSuiteSetupException;
import ws.performancetest.tests.jdbcdriver.runnable.cjdbc.CJDBCReadRunnable;
import static java.sql.ResultSet.*;

/**
 * Performance test of CResultSet with shared cache and individual cache.
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class CacheTest extends AbstractDriverTest {
  private CDriver cdriver = new CDriver();

  public CacheTest() throws TestSuiteSetupException, SQLException {
    super("CacheTest_Results", "CacheTest_Summary");
  }

  // <editor-fold defaultstate="collapsed" desc="Read">
  @PerformanceTest(name = "CJDBC_I - 10 %")
  public void testRIndividualCache10() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCI(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(10), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_I - 20 %")
  public void testRIndividualCache20() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCI(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(20), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_I - 50 %")
  public void testRIndividualCache50() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCI(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(50), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_I - 75 %")
  public void testRIndividualCache75() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCI(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(75), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_I - 100 %")
  public void testRIndividualCache100() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCI(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(100), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_SM - 10 %")
  public void testRSharedCache10() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(10), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_SM - 20 %")
  public void testRSharedCache20() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(20), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_SM - 50 %")
  public void testRSharedCache50() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(50), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_SM - 75 %")
  public void testRSharedCache75() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(75), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_SM - 100 %")
  public void testRSharedCache100() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                calcFetchSize(100), cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_S Oficial - 100 %")
  public void testRSharedNormalCache100() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY, cycleCount);
      }
    });

    tmpcon.close();
  }
  // </editor-fold>

  private int calcFetchSize(int perc) {
    return cycleCount * numberOfThreads * perc / 100;
  }
}
